package com.springboot_gradrecruit.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.springboot_gradrecruit.common.PageResponse;
import com.springboot_gradrecruit.common.R;
import com.springboot_gradrecruit.model.dto.collect.PageCollectRequest;
import com.springboot_gradrecruit.model.dto.job.PageJobRequest;
import com.springboot_gradrecruit.model.entity.Collect;
import com.springboot_gradrecruit.model.entity.Send;
import com.springboot_gradrecruit.model.vo.job.JobVO;
import com.springboot_gradrecruit.service.CollectService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zr
 */
@Tag(name = "收藏相关接口")
@RestController
@RequestMapping("/collect")
public class CollectController {


    @Resource
    private CollectService collectService;


    //增加
    @Operation(summary = "添加收藏")
    @PostMapping("/addCollect")
    public R<Boolean> addCollect(Long jobId, Long studentId){

        // 1. 参数校验
        if(jobId==null||studentId==null){
            throw new RuntimeException("参数为空");
        }
        // 2. 调用service完成添加
        Collect collect = new Collect();
        collect.setJobId(jobId);
        collect.setStudentId(studentId);
        return collectService.save(collect)?R.success(true,"收藏成功"):R.error(false,"收藏失败");

    }

    //删除
    @Operation(summary = "取消收藏")
    @PostMapping("/deleteCollect")
    public R<Boolean> deleteCollect(Long jobId, Long studentId){

        //1. 参数校验
        if (jobId == null || studentId == null) {
            throw new RuntimeException("参数为空");
        }

        //2. 调用service完成删除
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("job_id", jobId);
        queryWrapper.eq("student_id", studentId);
        return collectService.remove(queryWrapper)?R.success(true,"取消成功"):R.error(false,"取消失败");
    }

    //查看
    @Operation(summary = "查看是否已收藏")
    @PostMapping("/queryCollect")
    public R<Boolean> queryCollect(Long jobId, Long studentId){

        //1.参数校验
        if(jobId==null||studentId==null){
            return R.error(false,"参数为空");
        }
        //2.调用service完成查询
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("job_id",jobId);
        queryWrapper.eq("student_id",studentId);
        return collectService.exists(queryWrapper)?R.success(true,"已收藏"):R.success(false,"未收藏");
    }

    @Operation(summary = "分页获取已收藏的岗位")
    @PostMapping("/pageCollect")
    public R<PageResponse<JobVO>> pageCollect(@RequestBody PageCollectRequest pageCollectRequest, HttpServletRequest request){

        //1. 参数校验
        if(pageCollectRequest==null||request==null){
            throw new RuntimeException("参数为空");
        }
        System.out.println(pageCollectRequest);
        //2. 调用service完成查询
        PageResponse<JobVO> pageJob=collectService.pageCollect(pageCollectRequest);
        return R.success(pageJob,"查询成功");


    }

}































